#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/loadavg.h>
#include <unistd.h>
#include <time.h>

#define err() do { perror("server1"); exit(EXIT_FAILURE); } while(0)

int main(int argc, char *argv[])
{
	pid_t pid;
	uid_t uid;
	gid_t gid;
	int shmid;
	char * data;
	double load[3];
	time_t start;
	pid = getpid();
	uid = getuid();
	gid = getgid();
	start = time(NULL);
	if((shmid = shmget(7976, 1024, 0644 | IPC_CREAT)) == -1)
		err();
	for(;;)
	{
		data = shmat(shmid, (void *)0, 0);
		if(data == (char *)(-1)) err();
		if(getloadavg(load,3) == -1) err();
		sprintf(data,"pid:%ld\nuid:%ld\ngid:%ld\nseconds:%ld\n1:%f\t5:%f\t15:%f\n",pid,uid,gid,time(NULL)-start,load[0],load[1],load[2]);
		shmdt(data);
		sleep(1);
	}
	return 0;
}
